<template>
  <div v-loading="showLoading">
    <div>
      <div class="fx">
        <div class="block f5bg">
          <div class="block-left">纳税信用等级：</div>
          <div
            class="block-right"
            v-html-no="{ action: taxInformation.evaluationResult }"
          ></div>
        </div>
        <div class="block f5bg">
          <div class="block-left">纳税人种类：</div>
          <div
            class="block-right"
            v-html-no="{ action: taxInformation.qualification }"
          ></div>
        </div>
      </div>
      <div class="fx">
        <div class="block">
          <div class="block-left">历史有无欠税记录：</div>
          <div
            class="block-right"
            v-html-no="{ action: taxInformation.unpaidTaxAmount ? '有' : '无' }"
          ></div>
        </div>
        <div class="block">
          <div class="block-left">当前欠税金额：</div>
          <div
            class="block-right"
            v-html-no="{ action: taxInformation.unpaidTaxAmount }"
          ></div>
        </div>
      </div>
    </div>
    <div>
      <div style="font-weight: bold; color: #222222">纳税数据分析</div>
      <el-table style="margin-top: 10px" :data="enterpriseMonthPaidList" border>
        <el-table-column
          align="center"
          prop="name"
          label="纳税数据分析"
        ></el-table-column>
        <el-table-column
          align="center"
          v-for="month in monthList"
          :key="month"
          :label="monthLabel(month)"
        >
        <template slot-scope="scope">
          <div v-if="scope.row.name == '负债率' || scope.row.name == '利润率'" v-html-no="{ action: scope.row[month], unit: '%' }"></div>
          <div v-else v-html-no="{ action: scope.row[month] }"></div>
        </template>
      </el-table-column>
      </el-table>
    </div>

    <div class="canpanyer-box" style="margin-top: 20px">
      <div class="title-msg">近两年纳税信用等级</div>
      <el-table style="margin-top: 20px" :data="creditEvaluationList" border>
        <el-table-column
          align="center"
          prop="evaluationYear"
          label="评价年度"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="evaluationScore"
          label="评价得分"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="evaluationResult"
          label="评价结果"
        ></el-table-column>
        <el-table-column align="center" label="扣减分项">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.indicatorNames" :key="index">
              {{ item }}
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="canpanyer-box" style="margin-top: 20px">
      <div class="title-msg">财务情况</div>
      <el-table
        style="margin-top: 20px"
        :data="spiderFinanceInfosYearList"
        border
      >
        <el-table-column
          align="center"
          prop="name"
          label="财务数据分析"
        ></el-table-column>
        <el-table-column
          align="center"
          v-for="month in yearList"
          :key="month"
          :label="yearLable(month)"
        >
        <template slot-scope="scope">
          <div  v-html-no="{ action: scope.row[month] }"></div>
        </template>
      </el-table-column>
      </el-table>
    </div>
    <div class="canpanyer-box" style="margin-top: 20px">
      <div
        class="title-msg"
        style="display: flex; justify-content: space-between"
      >
        <div>近三年纳税信息</div>
        <el-radio-group @input="changeMessage" size="small" v-model="type">
          <el-radio-button :label="1">正税</el-radio-button>
          <el-radio-button :label="2">附加税</el-radio-button>
        </el-radio-group>
      </div>
      <month-table
        style="margin-top: 20px"
        :tableList="paidTaxesDataList"
      ></month-table>
    </div>
  </div>
</template>
<script>
import { getReportTax, getReportTaxNormal } from "@/api/report";
import { monthUtil } from "@/utils/date";
import monthTable from "@/components/monthTable/index.vue";
import { formatDateYear } from "@/utils/date";

export default {
  name: "taxInformation",
  components: {
    monthTable
  },
  data() {
    return {
      showLoading: false,
      taxInformation: {},
      //近三年纳税信息
      paidTaxesDataList: [],
      creditEvaluationList: [],
      //纳税分析列表
      enterpriseMonthPaidList: [],
      monthList: [],
      type: 1,

      objMap1: {
        unpaidTaxAmount: "欠税金额",
        taxSaleAmount: "纳税销售额",
        taxPaidAmount: "纳税实缴金额",
        coiled0Months: "0申报月数",
        debtRatio: "负债率",
        profitRatio: "利润率",
        vatPayableAmount: "增值税应纳额",
        taxIncomeAmount: "企业所得税",
        taxWaiverAmount: "减免税额",
        lateFeesAmount: "滞纳金金额",
        lateFeesNum: "滞纳金次数"
      },
      objMap2: {
        totalAssets: "资产合计",
        totalLiabilities: "负债合计",
        thisYearNetIncrease: "本年累计现金净增加额",
        thisYearFinalBalance: "本年期末现金余额",
        ownerEquity: "所有者权益合计",
        liabilitiesOwnerEquity: "负债和所有者权益总计",
        businessIncome: "营业收入",
        businessProfit: "营业利润",
        totalProfit: "利润总额",
        netProfits: "净利润"
      },
      spiderFinanceInfosYearList: [],
      yearList: []
    };
  },
  props: {
    detailInfo: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  created() {
    this.getEnterprisePaidTaxesAmountInfo();
    this.getReportTaxNormalInfo(1);
  },
  methods: {
    //正税
    getReportTaxNormalInfo(e = 1) {
      let params = {
        enterpriseId: this.detailInfo.id || obj.id,
        createTime: formatDateYear(new Date()),
        taxType: e
      };
      getReportTaxNormal(params).then(res => {
        console.log(res);
        this.paidTaxesDataList = res.data.map(v => {
          return {
            year: v.year,
            ...monthUtil(v.monthData),
            add: v.total
          };
        });
      });
    },
    //附加税
    changeMessage(e) {
      this.getReportTaxNormalInfo(e);
    },
    monthLabel(month) {
      switch (month) {
        case "month3":
          return "近3个月";
        case "month6":
          return "近6个月";
        case "month12":
          return "近12个月";
        case "month24":
          return "近24个月";
        default:
          return month;
      }
    },
    yearLable(year) {
      switch (year) {
        case "year2021":
          return "2021";
        case "year2022":
          return "2022";
        case "year2023":
          return "2023";
        default:
          return month;
      }
    },
    funData2(arr) {
      let objMap = {
        totalAssets: "资产合计",
        totalLiabilities: "负债合计",
        thisYearNetIncrease: "本年累计现金净增加额",
        thisYearFinalBalance: "本年期末现金余额",
        ownerEquity: "所有者权益合计",
        liabilitiesOwnerEquity: "负债和所有者权益总计",
        businessIncome: "营业收入",
        businessProfit: "营业利润",
        totalProfit: "利润总额",
        netProfits: "净利润"
      };
      const data = arr.map(item => {
        return {
          year: item.year,
          ...item.spiderFinanceInfoVo
        };
      });
      const result = Object.keys(objMap).map(key => ({
        name: objMap[key],
        ...data.reduce((acc, item) => {
          acc[`year${item.year}`] = null;
          return acc;
        }, {})
      }));
      data.forEach(item => {
        Object.entries(item).forEach(([key, value]) => {
          if (objMap[key]) {
            const resItem = result.find(r => r.name === objMap[key]);
            if (resItem) {
              resItem[`year${item.year}`] = value;
            }
          }
        });
      });

      let arrr = JSON.parse(JSON.stringify(result));
      const obj = delete arrr[0].name;
      this.yearList = Object.keys(arrr[0]);
      console.log(result, "result");
      return result;
    },
    funData(arr, type) {
      let objMap = {
        unpaidTaxAmount: "欠税金额",
        taxSaleAmount: "纳税销售额",
        taxPaidAmount: "纳税实缴金额",
        coiled0Months: "0申报月数",
        debtRatio: "负债率",
        profitRatio: "利润率",
        vatPayableAmount: "增值税应纳额",
        taxIncomeAmount: "企业所得税",
        taxWaiverAmount: "减免税额",
        lateFeesAmount: "滞纳金金额",
        lateFeesNum: "滞纳金次数"
      };

      // let objMap = {
      //   totalAssets: "资产合计",
      //   totalLiabilities: "负债合计",
      //   thisYearNetIncrease: "本年累计现金净增加额",
      //   thisYearFinalBalance: "本年期末现金余额",
      //   ownerEquity: "所有者权益合计",
      //   liabilitiesOwnerEquity: "负债和所有者权益总计",
      //   businessIncome: "营业收入",
      //   businessProfit: "营业利润",
      //   totalProfit: "利润总额",
      //   netProfits: "净利润"
      // };

      const data = arr.map(item => {
        return {
          month: item.month,
          ...item.taxEnterprisePaidDate
        };
      });
      const result = Object.keys(objMap).map(key => ({
        name: objMap[key],
        ...data.reduce((acc, item) => {
          acc[`month${item.month}`] = null;
          return acc;
        }, {})
      }));
      data.forEach(item => {
        Object.entries(item).forEach(([key, value]) => {
          if (objMap[key]) {
            const resItem = result.find(r => r.name === objMap[key]);
            if (resItem) {
              resItem[`month${item.month}`] = value;
            }
          }
        });
      });
      let arrr = JSON.parse(JSON.stringify(result));
      const obj = delete arrr[0].name;
      this.monthList = Object.keys(arrr[0]);
      return result;
    },
    getEnterprisePaidTaxesAmountInfo(id) {
      
      this.showLoading = true;
      let params = {
        enterpriseId: this.detailInfo.id || obj.id,
        createTime: formatDateYear(new Date()),
      };
      getReportTax(params)
        .then(res => {
          this.taxInformation = res.data;

          this.enterpriseMonthPaidList = this.funData(
            res.data.enterpriseMonthPaidList
          );
          console.log(res.data.spiderFinanceInfosYearList);
          this.spiderFinanceInfosYearList = this.funData2(
            res.data.spiderFinanceInfosYearList
          );
          this.creditEvaluationList = res.data.creditEvaluationList;
          this.showLoading = false;
        })
        .finally(() => {
          this.showLoading = false;
        });
    }
  }
};
</script>

<style scoped lang="scss">
.f5bg {
  background: #f5f5f5;
}
.fx {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  .block {
    width: 48%;
    display: flex;
    align-items: center;
    max-height: 60px;
    padding: 18px 0;

    //line-height: 56px;
    .block-left {
      display: inline-block;
      //display: flex;
      text-align: right;
      width: 60%;
      white-space: nowrap;
      color: #999;
    }
    .block-right {
      display: inline-block;
      //display: flex;
      line-height: 18px;
      color: #222;
      width: 38%;
    }
  }
}
.title-msg {
  font-size: 18px;
  font-weight: 500;
  color: #222222;
}
</style>
